# -*- coding: utf-8 -*-
'''
Subversion processor.

'''
from processors.single_repository_processor import SingleRepositoryProcessor
import pysvn
from processors.changeinfo import ChangeInfo
import datetime
from myutil import myutil
        

class SubversionProcessor(SingleRepositoryProcessor):   
    """
    Processor for subversion repository.
    """
    def _gather_log_info(self): #pylint: disable-msg=R0914
        """
        Retrieves changes from the repository.
        """
        def get_info(realm, username, may_save): #pylint: disable-msg=W0613
            """
            Callback for subversion to provide user and password.
            """
            return True, self.login, self.password, True
        def trust(trust_dict):
            """
            Callback for subversion to trust the repository.
            """
            return True, trust_dict['failures'], True 
        client = pysvn.Client()
        client.update(self.location)
        client.callback_get_login = get_info
        client.callback_ssl_server_trust_prompt = trust  
        logs = client.log(self.location, discover_changed_paths=True)
        for log in logs:
            author = log['author'] if 'author' in log else ''
            date = datetime.datetime.fromtimestamp(int(log['date']))
            email = myutil.get_mail(self.settings, author)
            changed = log['changed_paths']
            rev = log['revision'].number
            files = ", ".join([p['path'] for p in changed])
            comment = log['message']
            link = None
            if self.weburl is not None:
                if self.flavor == 'websvn':
                    link = self.weburl + "&rev=" + str(rev)  
                elif self.flavor == 'googlecode':
                    link = self.weburl + "source/detail?r=" + str(rev)
            change_info = ChangeInfo(rev, rev, author, email, \
                                     date, files, comment, link)
            self._append_change_info(change_info)
